package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.UserRegistContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.VerificationBean;

import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/22/2019 16:48
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class UserRegistPresenter extends BasePresenter<UserRegistContract.Model, UserRegistContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public UserRegistPresenter(UserRegistContract.Model model, UserRegistContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 获取验证码
     * @param maps
     */
    public void  message_new(Map<String,Object> maps) {
     mModel.regist_message_new(maps).compose(RxUtils.applySchedulers(mRootView))
     .subscribe(new ErrorHandleSubscriber<ResultBean<VerificationBean>>(mErrorHandler) {
         @Override
         public void onNext(ResultBean<VerificationBean> verificationBeanResultBean) {
           if (AppUtil.isSuccess(verificationBeanResultBean))
           {
               mRootView.showMessage("验证码发送成功");
               mRootView.queryVerificationSuccess(verificationBeanResultBean.getData());
           }else {
               mRootView.showMessage(verificationBeanResultBean.getMessage());
           }
         }
     });
    }

    /**
     * 注册点下一步校验验证码
     * @param mobile
     * @param identifyCode
     */
   public void  registerNext(String mobile, String identifyCode) {
        mModel.registerNext(mobile,identifyCode).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                     if (AppUtil.isSuccess(resultBean))
                     {
                         mRootView.registerNextSuccess();
                     }else {
                         mRootView.showMessage(resultBean.getMessage());
                     }
                     }
                });
   }
    /**
     * i请求验证码
     * @param phoneNum
     */
    public void queryVerification(String phoneNum,String sessionId, String appKey) {
        mModel.queryVerification(phoneNum,sessionId, appKey).compose(RxUtils.applySchedulers(mRootView)).subscribe(new ErrorHandleSubscriber<ResultBean<VerificationBean>>(mErrorHandler) {
            @Override
            public void onNext(ResultBean<VerificationBean> verificationBeanResultBean) {
                if (AppUtil.isSuccess(verificationBeanResultBean))
                {
                    mRootView.showMessage("验证码发送成功");
                    mRootView.queryVerificationSuccess(verificationBeanResultBean.getData());
                }else {
                    mRootView.showMessage(verificationBeanResultBean.getMessage());
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
